<template>
  <div>
    <el-form :model="searchList" ref="searchList" size="normal">
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="">
            <el-input v-model="searchList.project_name" placeholder="项目名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="8" :offset="0">
          <el-form-item label="">
            <el-input v-model="searchList.company_name" placeholder="创建部门" clearable></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="12" :offset="0">
          <div class="right">
            <el-button type='primary' size='medium' @click='search' :loading="loading">查询</el-button>
            <el-button type='primary' size='medium' @click='addProject'>添加项目</el-button>
            <el-button type="primary" size="medium" @click="handleExpand" :loading="loading">
              <i :class="isExpand?'el-icon-remove-outline':'el-icon-circle-plus-outline'"></i>
              {{isExpand?'关闭全部行':'展开全部行'}}
            </el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>

    <el-table :data='tableData' border stripe :height='offsetHeight' v-loading='loading' ref='table' row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label='名称' min-width='80' align='left' header-align="center" show-overflow-tooltip tree>
        <template slot-scope='{row}'>
          {{row.name}}
        </template>
      </el-table-column>
      <el-table-column label='金额' align='center' width="150">
        <template slot-scope='{row}'>
          <span v-if="row.grade =='1'">{{row.pr_service_amount ? row.pr_service_amount.toFixed(4):'0' }}</span>
          <span v-else-if="row.grade =='2'">{{row.pa_service_amount ? row.pa_service_amount.toFixed(4):'0' }}</span>
          <span v-else-if="row.grade =='3'">{{row.su_service_amount ? row.su_service_amount.toFixed(4):'0' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label='排序' width='80' align='center'>
        <template slot-scope='{row}'>
          <el-tag type="primary" size="mini" v-if="row.grade==2"> {{row.sort}}</el-tag>
          <el-tag type="info" size="mini" v-else-if="row.grade==3"> {{row.sort}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label='操作' width='400' align='center'>
        <template slot-scope='{row}'>
          <template v-if="row.grade=='1'">
            <el-button-group>
              <el-button type="primary" size="mini"  :disabled="row.send_status==1" @click="editProject(row)">修改</el-button>
              <el-button type="green" size="mini" :disabled="row.send_status==1" @click="addPackage(row)">添加包组</el-button>
<!--              <el-button type="success" :disabled="row.send_status==1" size="mini" @click="submit(row)">上报数字沈阳</el-button>-->
              <el-button type="danger"  :disabled="row.send_status==1" size="mini" @click="remove(row)">删除</el-button>
              <el-button type="success"   size="mini" @click="exportReport(row.id)" :loading="saveLoading">导出报告
              </el-button>
            </el-button-group>
          </template>
          <template v-else-if="row.grade=='2'">
            <el-button-group>
              <el-button type="primary" size="mini" @click="editPackage(row)">修改</el-button>
              <el-button type="green" size="mini" @click="pullBid(row)">拉取标的</el-button>
              <el-button type="danger" size="mini" @click="remove(row)">删除</el-button>
            </el-button-group>
          </template>
          <template v-else>
            <el-button-group>
              <el-button type="primary" size="mini" @click="editTarget(row)">修改</el-button>
              <el-button type="green" size="mini" @click="editIt(row)">查看IT资产及服务</el-button>
              <el-button type="warning" size="mini" @click="addTargetVar(row.id)">招标技术参数</el-button>
              <el-button type="danger" size="mini" @click="remove(row)">删除</el-button>
            </el-button-group>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <!-- 项目信息 -->
    <el-dialog title="招标项目信息" :visible.sync="projectShow" width="30%" @close="cancel">
      <project-item ref="project" @cancel="cancel" @init="init" :form-list="projectParams" :switch="this.switch">
      </project-item>
    </el-dialog>
    <!-- 包组信息 -->
    <el-dialog title="包组信息" :visible.sync="packageShow" width="30%" @close="cancel">
      <package-item ref="package" @cancel="cancel" @init="init" :form-list="packageParams" :switch="this.switch">
      </package-item>
    </el-dialog>
    <!-- 标的信息 -->
    <el-dialog title="标的信息" :visible.sync="targetShow" width="50%" @close="cancel" :destroy-on-close="true" top="10">
      <target-item ref="target" @cancel="cancel" @init="init" :form-list="targetParams" :switch="this.switch">
      </target-item>
    </el-dialog>
    <!-- 招标技术参数信息 -->
    <el-drawer title="招标技术参数" :visible.sync="targetVarShow" size="70%" :destroy-on-close="true"
      :wrapperClosable="false">
      <target-var-item ref="targetVar" @cancel="cancel" @init="init" :targetVarParams="targetVarParams" :switch="false">
      </target-var-item>
    </el-drawer>
    <!-- 标的信息 -->
    <el-dialog title="标的信息" :visible.sync="pullShow" width="80%" @close="init" :destroy-on-close="true" top="0">
      <pull-bid ref="pullBid" :pullParams="pullParams" @cancel="cancel">
      </pull-bid>
    </el-dialog>
    <!-- 项目上报咨询机构 -->
    <el-dialog title="项目上报" :visible.sync="thirdShow" width="50%" @close="cancel" :destroy-on-close="true">
      <el-form :model="thirdList" label-position="top" :rules="rules" size="normal">
        <el-form-item label="审核机构数量" prop="random_num">
          <el-input v-model="thirdList.random_num" placeholder="" size="normal">
            <template slot="append">个</template>
          </el-input>
        </el-form-item>
        <el-form-item label="不参与审核的咨询机构：">
          <el-checkbox-group v-model="thirdList.exclude_company_code">
            <el-checkbox v-for="item in thirdType" :key="item.value" :label="item.value">
              {{item.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <!-- <el-form-item label="审核意见" prop="check_advice">
          <el-input v-model="thirdList.check_advice" placeholder="" type="textarea" :rows="4"></el-input>
        </el-form-item> -->
      </el-form>
      <div class="center">
        <el-button type="danger" size="default" @click="cancel">取消</el-button>
        <el-button type="primary" size="default" @click="submit1" :loading="saveLoading">确认</el-button>
      </div>
    </el-dialog>
    <block-report-item v-show="false" ref="blockReport"></block-report-item>
  </div>
</template>
<script>
import ProjectItem from '@/components/Bid/Project.vue'
import PackageItem from '@/components/Bid/Package.vue'
import TargetItem from '@/components/Bid/Target.vue'
import TargetVarItem from '@/components/Bid/TargetVar.vue'
import RejectItem from '@/components/Bid/Reject.vue'
import BlockReportItem from '@/components/Report/BlockReport'
import PullBid from './pull-bid.vue'
export default {
  name: 'BidEnsureNoFinish',
  components: {
    ProjectItem, PackageItem, TargetItem, TargetVarItem, RejectItem, BlockReportItem, PullBid
  },
  props: {
    switch: {
      type: Boolean,
      default: () => {
        return false
      }
    }
  },
  data () {
    return {
      offsetHeight: document.documentElement.clientHeight - 250,
      loading: false,
      saveLoading: false,
      searchList: {},
      tableData: [],
      isExpand: false,
      projectShow: false,
      projectParams: {},
      packageShow: false,
      packageParams: {},
      targetShow: false,
      targetParams: {},
      targetVarShow: false,
      targetVarParams: {},
      pullShow: false,
      pullParams: {},
      thirdType: [],
      thirdShow: false,
      thirdList: {},
      rules: {
        random_num: [
          { required: true, message: '请输入审核机构数量', trigger: 'blur' },
        ],
        check_advice: [
          { required: true, message: '请输入审核意见', trigger: 'blur' },
        ]
      }
    }
  },
  created () {
  },
  mounted () {
    this.init()
  },
  methods: {
    async init () {
      this.loading = true
      const res = await this.$http('blockList', {
        del_flag: '0',
        status: '51',
        source: '2',
        del_flag: "0",
        order: 'ensure_code',
        ...this.searchList
      })
      this.tableData = res
      this.loading = false
    },
    search () {
      this.init()
    },
    pullBid (item) {
      this.pullParams = {
        id: item.id,
      }
      this.pullShow = true
    },
    addProject () {
      this.projectParams = {
        status: '51',
        source: '2'
      }
      this.projectShow = true
    },
    editProject (item) {
      this.projectParams = {
        id: item.id,
        project_name: item.name,
      }
      this.projectShow = true
    },
    addPackage (item) {
      this.packageParams = {
        project_id: item.id,
        project_name: item.name
      }
      this.packageShow = true
    },
    editPackage (item) {
      this.packageParams = {
        project_name: item.project_name,
        id: item.id,
        package_name: item.name,
        sort: item.sort
      }
      this.packageShow = true
    },
    addTarget (item) {
      this.targetParams = {
        project_name: item.project_name,
        package_name: item.name,
        package_id: item.id,
      }
      this.targetShow = true
    },
    editTarget (item) {
      this.targetParams = {
        project_name: item.project_name,
        package_name: item.name,
        id: item.id,
        target_name: item.name,
        serve_time: item.serve_time,
        type: JSON.parse(item.type),
        type_other: item.type_other,
        sort: item.sort,
        content: item.content
      }
      this.targetShow = true
    },
    addTargetVar (id) {
      this.targetVarParams = {
        id: id,
      }
      this.targetVarShow = true
    },
    editIt (item) {
      sessionStorage.setItem("bidInfo",
        JSON.stringify({
          id: item.id,
          source: '2',
          titleName: item.name + '——' + item.package_name + '——' + item.project_name,
        })
      );
      this.$emit('toIt', this.switch, 'noFinish');
    },
    async submit (item) {
      //数据会推送至数字沈阳政务信息化项目监督管理平台，进行拦标价后期流程审批
      this.$confirm('确定上报至数字沈阳政务信息化项目监督管理平台吗', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async action => {
        this.loading = true
        const res = await this.$http('sendDigtalsypmplatMaintainData', {
          id: item.id
        })
        this.loading = false
        this.$tips.success(res.errmsg)
        this.init()
      }).catch(() => {

      });
      // this.thirdList = {
      //   id: item.id,
      //   status: item.status,
      //   exclude_company_code: []
      // }
      // const res = await this.$http('sysThirdList', {
      //
      // })
      // let list = []
      // res.forEach(item => {
      //   if (item.maintain_check_flag === '0') {
      //     list.push({ value: item.code, label: item.name })
      //   }
      // })
      // this.thirdType = list
      // this.thirdShow = true
    },
    async submit1 () {
      this.saveLoading = true
      const regex = /^\+?[1-9]\d*$/
      if (!regex.test(this.thirdList.random_num)) {
        this.$tips.error('请填写整数')
        this.saveLoading = false
        return false
      }
      // if (!this.thirdList.check_advice) {
      //   this.$tips.error('请填写审核信息')
      //   return false
      // }
      this.$confirm('是否确认提交', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async action => {
        const res = await this.$http('blockProjectUp', {
          del_flag: '0',
          source: '2',
          target_status: '4',
          check_status: '1',
          id: this.thirdList.id,
          status: this.thirdList.status,
          random_num: this.thirdList.random_num,
          exclude_company_code: this.$util.arrToStr(this.thirdList.exclude_company_code),
          check_advice: this.thirdList.check_advice
        })
        if (res.errcode == 4000) {
          // let errmsg = this.getInitErrorMsg(res)
          this.$tips.error(res.errmsg)
          this.saveLoading = false
        } else {
          this.$tips.success(res.errmsg)
          this.saveLoading = false
          this.cancel()
          this.init()
        }
      }).catch(() => {
        this.saveLoading = false
      });
    },
    async remove (item) {
      this.$confirm('是否删除', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async action => {
        const res = await this.$http('blockRootDel', {
          del_flag: '1',
          grade: item.grade,
          id: item.id
        })
        this.$tips.success(res.errmsg)
        this.init()
      }).catch(() => {

      });

    },
    async exportReport (id) {
      this.saveLoading = true
      await this.$refs.blockReport.writerUpdate(id)
      this.saveLoading = false
    },
    cancel () {
      this.projectShow = false
      this.projectParams = {}
      this.packageShow = false
      this.packageParams = {}
      this.targetShow = false
      this.targetParams = {}
      this.targetVarShow = false
      this.targetVarParams = {}
      this.pullShow = false
      this.thirdList = {
        exclude_company_code: []
      }
      this.thirdShow = false
    },
    // 提示信息
    getInitErrorMsg (data) {
      let parse = JSON.parse(data.errmsg)
      let errormsg = '操作失败 <br/>'
      for (let i = 0; i < parse.length; i++) {
        if (parse[i].projectName == null) {
          errormsg = '该项目为空'
          return errormsg
        }
        if (parse[i].packageName == null) {
          errormsg = '项目：' + parse[i].projectName + '- 包组为空'
          return errormsg
        }
        if (parse[i].subjectName == null) {
          errormsg = '项目：' + parse[i].projectName + '- 包组：' + parse[i].packageName + '- 标的为空'
          return errormsg
        }
        if (parse[i].content == null) {
          errormsg = '项目：' + parse[i].projectName + '- 包组：' + parse[i].packageName + '- 标的：' + parse[i].subjectName + '- 招标技术参数为空'
          return errormsg
        }
        if (parse[i].type == null || parse[i].type === '') {
          errormsg = '项目：' + parse[i].projectName + '- 包组：' + parse[i].packageName + '- 标的：' + parse[i].subjectName + '- 该项目标的为空'
          return errormsg
        }
      }
    },
    // 是否展开table(展开与折叠切换)
    handleExpand () {
      this.isExpand = !this.isExpand
      this.$nextTick(() => {
        this.forArr(this.tableData, this.isExpand)
      })
    },
    // 遍历
    forArr (arr, isExpand) {
      arr.forEach(i => {
        // toggleRowExpansion(i, isExpand)用于多选表格，切换某一行的选中状态，如果使用了第二个参数，则是设置这一行选中与否（selected 为 true 则选中）
        this.$refs.table.toggleRowExpansion(i, isExpand)
        if (i.children) {
          this.forArr(i.children, isExpand)
        }
      })
    },
  },
  computed: {

  },
}
</script>
<style lang='scss' scoped>
</style>
